package com.twu.biblioteca;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: PREETHI
 * Date: 22/5/13
 * Time: 4:11 PM
 * To change this template use File | Settings | File Templates.
 */
public class framemovies {
    library lib;
    JScrollPane movieScrollPane;
    JTable movieTable;

    String colHeaders[]={"Movie","Year","Director","Rating","Reserve"};
    Object data[][];
    public framemovies(library lib)
    {
        this.lib=lib;
        lib.addMovies();
    }


    public JScrollPane listMovies()
    {
        movieScrollPane=new JScrollPane();
        List<movie> ml= lib.getMovieList();
        int size = ml.size();
        data=new Object[size][5];
        for(int i=0;i<size;i++)
        {
            movie m= ml.get(i);

            data[i][0]=new String(m.getMovieName());
            data[i][1]=new Integer(m.getYear());
            data[i][2]=new String(m.getDirector());
            data[i][3]=new String(m.getRating());
            data[i][4]=new JButton("RESERVE");
        }
        DefaultTableModel dm=new DefaultTableModel(data,colHeaders);
        movieTable=new JTable(dm){
            public boolean isCellEditable (int iRows, int iCols) {
                if(iCols==4)
                    return true;
                return false;
            }
            public void tableChanged(TableModelEvent e) {
                super.tableChanged(e);
                repaint();
            }
        };
        movieTable.getColumn("Reserve").setCellRenderer(new ButtonRenderer());
        movieTable.getColumn("Reserve").setCellEditor(new ButtonEditor(new JCheckBox()));
        movieScrollPane.setViewportView(movieTable);
        return movieScrollPane;

    }
    class ButtonRenderer implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

            if (value == null)
            {
                return null;
            }
            return (Component) value;


        }
    }
    class ButtonEditor extends DefaultCellEditor implements ActionListener {
        private JButton button;
        public int movieIndex;
        public ButtonEditor(JCheckBox checkBox) {
            super(checkBox);
        }

        public Component getTableCellEditorComponent(JTable table, Object value,
                                                     boolean isSelected, int row, int column) {
            if (value == null)
                return null;
            button = (JButton) value;

            movieIndex=row;
            button.addActionListener(this);
            return (Component) value;
        }

        public Object getCellEditorValue() {
            button.removeActionListener(this);
            return button;
        }

        public void actionPerformed(ActionEvent e) {

            movie  m= lib.getMovieList().get(movieIndex);
            reserveMovies(m);
        }
    }

    public int reserveMovies(movie m) {

        if(lib.getUsername() !=null)
        {
            if(m.getQuantity() >0)
            {
                JOptionPane.showMessageDialog(movieScrollPane,"Thank you! Enjoy the movie!","Reserved",JOptionPane.INFORMATION_MESSAGE);
                m.setQuantity(m.getQuantity() - 1);
                return 1;
            }
            else
            {
                JOptionPane.showMessageDialog(movieScrollPane,"Sorry!The movie is not available yet!","Unavailable",JOptionPane.INFORMATION_MESSAGE);
                return 0;

            }
        }
        else
            JOptionPane.showMessageDialog(movieScrollPane,"You must login to reserve movie","Failure",JOptionPane.INFORMATION_MESSAGE);
        return 0;
    }
}

